#!/bin/bash
#自动工具启动时初始化脚本
#参数：系统操作根路径
#dir=$(cd `dirname $0`; pwd)

error_exit(){
	echo "`date +%Y:%m:%d-%H:%M:%S` ERROR: $1" >> ${logFile}
	echo "ERROR: 初始化工作目录, $1"
	exit 1
}

rootPath=$1
dir_name=$2
logFile="${rootPath}/${dir_name}/init.log"
cd ${rootPath}

#判断文件目录是否存在
if [ -e ${dir_name} ]
then
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir ${dir_name} is exist " >> ${logFile}
else
	mkdir ${dir_name} || error_exit "创建文件夹${dir_name}失败"
	chmod 770 ${dir_name}/
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir ${dir_name}! " >> ${logFile}
fi

cd ${dir_name}
if [ -e logs ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir logs is exist " >> ${logFile}
else
	mkdir logs || error_exit "创建文件夹logs失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir logs! " >> ${logFile}
fi

if [ -e authorize ]
then
    echo "`date +%Y:%m:%d-%H:%M:%S` INFO: the dir authorize is exist " >> ${logFile}
else
	mkdir authorize || error_exit "创建文件夹authorize失败"
	echo "`date +%Y:%m:%d-%H:%M:%S` INFO: create dir authorize! " >> ${logFile}
fi